package com.github.m6d21.sso.dao.role.dao.impl;

import com.github.m6d21.sso.dao.role.dao.IRoleDao;
import com.github.m6d21.sso.dao.role.mapper.RoleMapper;
import com.github.m6d21.sso.model.role.entity.Role;
import com.github.m6d21.sso.model.role.qo.RoleQO;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Repository;

/**
 *<p>
 *
 *</p>
 *
 * @author liuyang
 * @date 2019/1/4 Fri 17:00:00
 * @since 1.0.0
 */
@Repository("roleDao")
public class RoleDaoImpl implements IRoleDao {

    @Resource
    private RoleMapper roleMapper;

    @Override
    public Role getById(Integer id) {
        if (id == null) {
            return null;
        }
        return roleMapper.get(id);
    }

    @Override
    public boolean insertRole(Role role) {
        if (role == null) {
            return false;
        }
        int row = roleMapper.insertRole(role);
        return row == 1;
    }

    @Override
    public boolean insertSelectiveRole(Role role) {
        if (role == null) {
            return false;
        }
        int row = roleMapper.insertSelectiveRole(role);
        return row == 1;
    }

    @Override
    public boolean updateRoleById(Role role) {
        if (role == null || role.getId() == null) {
            return false;
        }
        int row = roleMapper.updateRoleById(role);
        return row == 1;
    }

    @Override
    public boolean updateSelectiveRoleById(Role role) {
        if (role == null || role.getId() == null) {
            return false;
        }
        int row = roleMapper.updateSelectiveRoleById(role);
        return row == 1;
    }

    @Override
    public List<Role> find(RoleQO roleQO) {
        if(roleQO==null){
            return new ArrayList<>(0);
        }
        return roleMapper.find(roleQO);
    }

    @Override
    public List<Role> query(RoleQO roleQO) {
        if (roleQO == null) {
            roleQO = new RoleQO();
        }
        return roleMapper.query(roleQO);
    }

    @Override
    public List<Role> queryForAdmin(RoleQO roleQO) {
        if (roleQO == null) {
            roleQO = new RoleQO();
        }
        roleQO.setDisable(0);
        return roleMapper.query(roleQO);
    }

    @Override
    public boolean deleteById(Integer id) {
        if (id == null) {
            return false;
        }
        int row = roleMapper.deleteRoleById(id);
        return row == 1;
    }

    @Override
    public boolean deleteByIds(List<Integer> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        int row = roleMapper.deleteRoleByIds(ids);
        return row == ids.size();
    }
}
